// LSL script generated: RLV-New.RLV_MENU.DV_RLV_Shared.lslp Sun May  2 00:09:51 Westeuropäische Sommerzeit 2010


integer attachment = 1;
string cmdname;
key victim;
list gMenu = [];
key ToucherID;
integer channel_dialog;
integer listen_id;
integer listen_id2;
string msg = "Shared Folder";
integer gPage;
list gPlace = [];
integer steps;
string TICKED = "(*)";
string STICKED = "(.)";
string UNTICKED = "( )";
string FOLDER = " / ";
list shortfolders = [];
list longfolders = [];
string ATTACH_ALL = "(+) *All*";
string DETACH_ALL = "(-) *All*";
string ATTACH_THIS = "(+) *This*";
string DETACH_THIS = "(-) *This*";
list pageMenu(list in,integer page){
    list out;
    integer maxpages = (llGetListLength(in) / 9);
    (page = (page % (maxpages + 1)));
    (out = ["<<","[Back]",">>"]);
    integer _listcount = ((page * 9) + 9);
    integer _i = (_listcount - 9);
    for (_i; (_i < _listcount); (_i++)) {
        if (((_i <= llGetListLength(in)) && (llList2String(in,_i) != ""))) (out += llList2String(in,_i));
    }
    return out;
}

llRlvSay(string command){
    if ((attachment == 1)) {
        llOwnerSay(("@" + command));
    }
    else  {
        llSay(-1812221819,(((((cmdname + ",") + ((string)victim)) + ",") + "@") + command));
    }
}

MakeRLVState(){
    if ((attachment == 1)) {
    }
}

makeMenu(){
    (gMenu = [" "]);
    llRlvSay("getinvworn=13337");
    (listen_id2 = llListen(13337,"",NULL_KEY,""));
    llSetTimerEvent(45.0);
}

displayMenu(){
    list _menu;
    if ((llGetListLength(gMenu) < 10)) {
        (_menu = ([" ","[Back]"," "] + gMenu));
    }
    else  if ((llGetListLength(gMenu) >= 10)) {
        (_menu = pageMenu(gMenu,gPage));
    }
    (channel_dialog = (-1 * ((integer)("0x" + llGetSubString(((string)llGetKey()),-5,-1)))));
    (listen_id = llListen(channel_dialog,"",NULL_KEY,""));
    llDialog(ToucherID,msg,_menu,channel_dialog);
    llSetTimerEvent(45.0);
}

default {

    state_entry() {
    }

    
    listen(integer channel,string name,key id,string message) {
        if ((channel == 13337)) {
            (longfolders = []);
            (shortfolders = []);
            list data = llParseStringKeepNulls(message,[","],[]);
            string first = llList2String(data,0);
            (data = llListSort(llList2List(data,1,-1),1,1));
            integer i;
            list item;
            integer worn;
            for ((i = 0); (i < llGetListLength(data)); (i++)) {
                (item = llParseString2List(llList2String(data,i),["|"],[]));
                string folder = llList2String(item,0);
                (worn = llList2Integer(item,1));
                if (((worn % 10) >= 1)) {
                    (longfolders += folder);
                    (shortfolders += [llGetSubString((FOLDER + folder),0,20)]);
                    (msg += (folder + "\n"));
                }
                else  if ((worn == 10)) {
                    (longfolders += folder);
                    (shortfolders += [llGetSubString((UNTICKED + folder),0,20)]);
                    (msg += (folder + "\n"));
                }
                else  if ((worn == 20)) {
                    (longfolders += folder);
                    (shortfolders += [llGetSubString((STICKED + folder),0,20)]);
                    (msg += (folder + "\n"));
                }
                else  if ((worn == 30)) {
                    (longfolders += folder);
                    (shortfolders += [llGetSubString((TICKED + folder),0,20)]);
                    (msg += (folder + "\n"));
                }
            }
            (item = llParseString2List(first,["|"],[]));
            (worn = llList2Integer(item,0));
            if (((worn % 10) == 1)) {
                (shortfolders += [ATTACH_ALL]);
            }
            else  if (((worn % 10) == 2)) {
                (shortfolders += [ATTACH_ALL,DETACH_ALL]);
            }
            else  if (((worn % 10) == 3)) {
                (shortfolders += [DETACH_ALL]);
            }
            if (((worn / 10) == 1)) {
                (shortfolders += [ATTACH_THIS]);
            }
            else  if (((worn / 10) == 2)) {
                (shortfolders += [ATTACH_THIS,DETACH_THIS]);
            }
            else  if (((worn / 10) == 3)) {
                (shortfolders += [DETACH_THIS]);
            }
            (gMenu = shortfolders);
            llListenRemove(listen_id2);
            displayMenu();
        }
        if ((channel == channel_dialog)) {
            llListenRemove(listen_id);
            if ((message == "<<")) {
                (gPage--);
                integer maxpages = (llGetListLength(gMenu) / 9);
                if ((gPage < 0)) {
                    (gPage = maxpages);
                }
                displayMenu();
            }
            else  if ((message == ">>")) {
                (gPage++);
                displayMenu();
            }
            else  if ((message == "Attach")) {
                string _place;
                integer _j = 0;
                (_place += llList2String(gPlace,_j));
                (_j++);
                for (_j; (_j < steps); (_j++)) {
                    (_place += ("/" + llList2String(gPlace,_j)));
                }
                llRlvSay((("attachall:" + _place) + "=force"));
                llRlvSay(((("getinvworn:" + _place) + "=") + "13337"));
                (listen_id2 = llListen(13337,"",NULL_KEY,""));
                llSetTimerEvent(45.0);
            }
            else  if ((message == "Detach")) {
                string _place;
                integer _j = 0;
                (_place += llList2String(gPlace,_j));
                (_j++);
                for (_j; (_j < steps); (_j++)) {
                    (_place += ("/" + llList2String(gPlace,_j)));
                }
                llRlvSay((("detachall:" + _place) + "=force"));
                llRlvSay(((("getinvworn:" + _place) + "=") + "13337"));
                (listen_id2 = llListen(13337,"",NULL_KEY,""));
                llSetTimerEvent(45.0);
            }
            else  if ((message == ATTACH_ALL)) {
                string _place;
                integer _j = 0;
                for (_j; (_j < steps); (_j++)) {
                    (_place += (llList2String(gPlace,_j) + "/"));
                }
                llRlvSay((("attachall:" + _place) + "=force"));
                llRlvSay(((("getinvworn:" + _place) + "=") + "13337"));
                (listen_id2 = llListen(13337,"",NULL_KEY,""));
                llSetTimerEvent(45.0);
            }
            else  if ((message == DETACH_ALL)) {
                string _place;
                integer _j = 0;
                for (_j; (_j < steps); (_j++)) {
                    (_place += (llList2String(gPlace,_j) + "/"));
                }
                llRlvSay((("detachall:" + _place) + "=force"));
                llRlvSay(((("getinvworn:" + _place) + "=") + "13337"));
                (listen_id2 = llListen(13337,"",NULL_KEY,""));
                llSetTimerEvent(45.0);
            }
            else  if ((message == ATTACH_THIS)) {
                string _place;
                integer _j = 0;
                for (_j; (_j < steps); (_j++)) {
                    (_place += (llList2String(gPlace,_j) + "/"));
                }
                llRlvSay((("attach:" + _place) + "=force"));
                llRlvSay(((("getinvworn:" + _place) + "=") + "13337"));
                (listen_id2 = llListen(13337,"",NULL_KEY,""));
                llSetTimerEvent(45.0);
            }
            else  if ((message == DETACH_THIS)) {
                string _place;
                integer _j = 0;
                for (_j; (_j < steps); (_j++)) {
                    (_place += (llList2String(gPlace,_j) + "/"));
                }
                llRlvSay((("detach:" + _place) + "=force"));
                llRlvSay(((("getinvworn:" + _place) + "=") + "13337"));
                (listen_id2 = llListen(13337,"",NULL_KEY,""));
                llSetTimerEvent(45.0);
            }
            else  if ((message == "[Back]")) {
                (gPlace = llDeleteSubList(gPlace,(steps - 1),(steps - 1)));
                if ((steps > 0)) {
                    (steps--);
                    string _place;
                    integer _j = 0;
                    for (_j; (_j < steps); (_j++)) {
                        (_place += (llList2String(gPlace,_j) + "/"));
                    }
                    (msg = (("Path:#RLV/" + _place) + "\n"));
                    llRlvSay(((("getinvworn:" + _place) + "=") + "13337"));
                    (listen_id2 = llListen(13337,"",NULL_KEY,""));
                    llSetTimerEvent(45.0);
                }
                else  {
                    llMessageLinked(-4,1337,"DV_MAIN_RLV",ToucherID);
                }
            }
            else  {
                string _stripped;
                integer _i = llListFindList(gMenu,[message]);
                string preComand = llGetSubString(message,0,2);
                (gPlace += [llList2String(longfolders,_i)]);
                (steps++);
                string _place;
                integer _j = 0;
                for (_j; (_j < steps); (_j++)) {
                    (_place += (llList2String(gPlace,_j) + "/"));
                }
                (msg = (("Path:#RLV/" + _place) + "\n"));
                if ((preComand == FOLDER)) {
                    llRlvSay(((("getinvworn:" + _place) + "=") + "13337"));
                    (listen_id2 = llListen(13337,"",NULL_KEY,""));
                    llSetTimerEvent(45.0);
                }
                else  {
                    (gMenu = []);
                    if ((preComand == TICKED)) {
                        (gMenu += "Detach");
                        displayMenu();
                    }
                    else  if ((preComand == STICKED)) {
                        (gMenu += "Attach");
                        (gMenu += "Detach");
                        displayMenu();
                    }
                    else  if ((preComand == UNTICKED)) {
                        (gMenu += "Attach");
                        displayMenu();
                    }
                }
            }
        }
    }

    
    on_rez(integer on) {
    }

    
    link_message(integer sender,integer num,string str,key id) {
        if ((num == 1337)) {
            if ((str == llGetScriptName())) {
                llMessageLinked(-4,1338,llGetScriptName(),NULL_KEY);
                (ToucherID = id);
                (gPlace = []);
                (steps = 0);
                makeMenu();
                (msg = "Path:#RLV/\n");
            }
        }
        if ((num == 1338)) {
            if ((str == "attachment")) (attachment = 1);
            else  if ((str == "device")) (attachment = 0);
            else  if ((str != llGetScriptName())) {
                llListenRemove(listen_id);
                llListenRemove(listen_id2);
            }
        }
        if ((num == 1339)) {
            if ((str == "ON_REZ")) {
                MakeRLVState();
            }
            if ((str == "RESET")) {
            }
        }
        if ((num == 1329)) {
            (victim = ((key)str));
            (cmdname = ((string)llGetKey()));
        }
    }

  	
  	timer() {
        llSetTimerEvent(0.0);
        llListenRemove(listen_id);
        llListenRemove(listen_id2);
    }
}
